// pages/login/index.js
const app = getApp()
const defaultAvatarUrl = "../../assets/my-avatar.png"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('=============user',wx.canIUse('button.open-type.getUserInfo'))
  },
  onChooseAvatar(e) {
    console.log('onChooseAvatar======',e.detail)
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
    app.globalData.userInfo = this.data.userInfo
    wx.login({
      success: (res) => {
        console.log('login============',res)
        const { code } = res
        app.request({
          url: `/api/user/login`,
          data:  {
            email:'001@talk.com',
            password:'001',
            wechat_code:code
          },
          method: "post",
        }).then(res=>{
          console.log('========登录完成========')
          app.globalData.isLogin = true
          wx.switchTab({
            url: '/pages/AIteacher/index',
          })
        })
      },
    })
   
  },
  getUserProfile(e){
     // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        // this.setData({
        //   userInfo: res.userInfo,
        //   hasUserInfo: true
        // })
        console.log('getUserProfile=============',res)

        wx.login({
          success (res) {
            if (res.code) {
              //发起网络请求
              console.log('登录成功，发起网络请求',res)
              app.request({
                url: `/api/user/login`,
                data:  {
                  email:'001@talk.com',
                  password:'001'
                },
                method: "post",
              }).then(res=>{
                console.log('========登录完成========')
                app.globalData.isLogin = true
                wx.switchTab({
                  url: '/pages/AIteacher/index',
                })
              })
            } else {
              console.log('登录失败！' + res.errMsg)
            }
          }
        })
      }
    })
  
  },
  getUserInfo(e) {
    console.log(e.detail ,  ">>>>>>number");
    wx.getUserInfo({
      success: function(res) {
        var userInfo = res.userInfo
        var nickName = userInfo.nickName
        var avatarUrl = userInfo.avatarUrl
        var gender = userInfo.gender //性别 0：未知、1：男、2：女
        var province = userInfo.province
        var city = userInfo.city
        var country = userInfo.country
      }
    })
  },
  getPhoneNumber(e) {
    console.log(e.detail.errMsg, ">>>>>>number");
    console.log(e.detail.iv);
    console.log(e.detail.encryptedData, "number<<<<<<<");
    console.log('app.globalData',app.globalData)
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let options = {};
      options.loading = true;
      options.url = "/api/app/open/wxapp/createByPhoneNumber";
      options.data = {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        category: 0,
        openid: app.globalData.openId,
        unionid: app.globalData.unionId
      };
 
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})